package com.huaxinkai.mobile.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.huaxinkai.entity.UserEntity;
import com.huaxinkai.mobile.service.IUserService;
@Controller
@RequestMapping(value="/user")
public class UserController extends MBaseController{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserService userService;
	
	/**
	 * 用户注册
	 * @return
	 */
	@RequestMapping(value="/register.htm")
	public String register(){
		return "user/register";
		
	}
	/**
	 * 增加User
	 * @return boolean
	 */
	@RequestMapping(value="/addUser.htm")
	public String addUser(
			@RequestParam("name")String name,
			@RequestParam("password")String password,
			Model model){
		UserEntity u = new UserEntity();
		u.setName(name);
		u.setPassword(password);
		u.setIdNumber("522632199205104339");
		u.setPhone("15997894263");
		u.setAddress("wuhanshi");
		try {
			userService.addUser(u);
			model.addAttribute("USER", u);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "user";
	}
	/**
	 * 用户登录
	 * @return
	 */
	@RequestMapping(value="/login.htm")
	public String login(){
		return "user/login";
	}
	/**
	 * 获取User
	 * @return List<User>
	 */
	@RequestMapping(value="/getUser.htm")
	public String getUser(
			@RequestParam("name")String name,
			@RequestParam("password")String password,
			HttpServletRequest req,
			HttpSession session, 
			Model model){
		try {
			req.setCharacterEncoding("UTF-8");
			UserEntity u = userService.getUser(name,password);
			System.out.println(u);
			if(u != null){
				model.addAttribute("res", "登录成功！！");
				session.setAttribute("USER", u);
			}else{
				model.addAttribute("res", "登录失败！！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "user";
	}
	/**
	 * 获取单个user
	 * @return
	 */
	@RequestMapping(value="/getUserById.htm")
	public String getUserById(@RequestParam("id") int id,Model model){
		try {
			model.addAttribute("USER", userService.getUser(id));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "user";
	}
	/**
	 * 聊天界面
	 * @return
	 */
	@RequestMapping(value="/chat.htm")
	public String chat(){
		
		return "user/chat";
	}

	
}
